package com.lngsyz.eseal.base.biz.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.eseal.base.redis.RedisUtil;
import com.lngsyz.eseal.base.util.CommonUtil;

@Service
public class RedisService {
    
	@Autowired
	RedisUtil redisUtil;

	// 记录用户最近一次操作时间的[14分钟]
	private final long REDIS_EXPIRE_SECOND = 840; 
	// 记录动态验证码和手机验证码的[5分钟]
	private final long REDIS_CODE_EXPIRE_SECOND = 300; 

	public final String PRE_MOBILE_TOKEN = "ESEAL_MOBILE_TOKEN_";

	public final String PRE_VERIFY_TOKEN = "ESEAL_VERIFY_TOKEN_";

	public void refreshTemp(String id) {
		redisUtil.set(id, id, REDIS_EXPIRE_SECOND);
	}
	
	public boolean getTemp(String id) {
		Object o = redisUtil.get(id);
		if(o == null) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 
	 * @param mobile
	 * @param code
	 * @param type [1:注册 2:密码找回]
	 * @return
	 */
	public void setMobileCode(String mobile, String code, String type) {
		redisUtil.set(PRE_MOBILE_TOKEN + mobile + type, code, REDIS_CODE_EXPIRE_SECOND);
	}
	
	public String getMobileCode(String mobile, String type) {
		String key = PRE_MOBILE_TOKEN + mobile + type;
		Object o = redisUtil.get(key);
		if(o != null) {
			redisUtil.del(key);
			return o.toString();
		} else {
			return null;
		}
	}
	
	public String setVerifyCode(String code, String type) {
		String id = CommonUtil.createUuid();
		redisUtil.set(PRE_VERIFY_TOKEN + id + type, code, REDIS_CODE_EXPIRE_SECOND);
		return id;
	}

	public String getVerifyCode(String id, String type) {
		String key = PRE_VERIFY_TOKEN + id + type;
		Object o = redisUtil.get(key);
		if(o != null) {
			redisUtil.del(key);
			return o.toString();
		} else {
			return null;
		}
	}
}
